<?php

namespace app\zyhsapi\controller\v1;

use app\common\lib\Util;
use app\zyhsapi\controller\ZyhsAuth;
use app\zyhsapi\model\ZyhsAstroSettingUser;

/**
 * 星盘配置相关接口
 */
class Astroset extends ZyhsAuth
{
    const DEFAULT_CONFIGURATION = [
        'planet' => [
            0 => [0, 0],
            1 => [0, 0],
            2 => [0, 0],
            3 => [0, 0],
            4 => [0, 0],
            5 => [0, 0],
            6 => [0, 0],
            7 => [0, 0],
            8 => [0, 0],
            9 => [0, 0],
            10 => [0, 0],
            11 => [0, 0],
            12 => [0, 0],
            13 => [0, 0],
            14 => [0, 0],
            15 => [0, 0],
            16 => [0, 0],
            17 => [0, 0],
            18 => [0, 0],
            19 => [0, 0],
            20 => [0, 0]
        ],
        'phase' => [
            0 => [0, 0],
            60 => [0, 0],
            90 => [0, 0],
            120 => [0, 0],
            180 => [0, 0],
            45 => [0, 0],
            135 => [0, 0],
            30 => [0, 0],
            150 => [0, 0],
            72 => [0, 0],
            144 => [0, 0],
            108 => [0, 0],
            36 => [0, 0],
        ],
        'arabic' => [
            0 => [0, 0],
            1 => [0, 0],
            2 => [0, 0],
            3 => [0, 0],
            4 => [0, 0],
            5 => [0, 0],
            6 => [0, 0],
            7 => [0, 0],
            8 => [0, 0],
            9 => [0, 0],
            10 => [0, 0],
            11 => [0, 0],
            12 => [0, 0],
            13 => [0, 0],
            14 => [0, 0],
            15 => [0, 0],
            16 => [0, 0],
            17 => [0, 0],
            18 => [0, 0],
            19 => [0, 0],
            20 => [0, 0],
            21 => [0, 0],
            22 => [0, 0],
            23 => [0, 0],
            24 => [0, 0],
            25 => [0, 0],
            26 => [0, 0],
        ],
        'h_sys' => 'p',
        'ecliptic' => 0,
        'zodrel' => 'p17',
        'firdaria' => 0,
        'profection_push_direction' => 0,
        'decanates' => 0,
        'bound_type' => 0,
        'ayanamsa' => 0,
        'location_use' => 0,
        'mid_type' => 0,// 行星中点配置 0=>近中点，1=>远中点，2=>不考虑
    ];

    /**
     * 获取星盘配置
     * @method GET
     * @return \think\response\Json
     */
    public function index()
    {
        $param = request()->only(['tid', 'chart']);

        $validate = validate('Astroset');

        if (!$validate->scene('index')->check($param))
        {
            return show(config('code.error'), (string)$validate->getError());
        }

        $param['chart'] = strtolower($param['chart']);
        $map = ['uid' => $this->user['id'], 'chart' => $param['chart'], 'tid' => $param['tid']];

        $AstroSettingUserModel = new ZyhsAstroSettingUser();

        $data = $AstroSettingUserModel->getUserSettingByTid($map);

        $astroConfig = config('astroset1751');
        if (isset($data['asp_orb_all']) && $data['asp_orb_all'])
        {
            $arr = $this->OldAstroData($param, $data, $astroConfig);
        } else
        {
            if (count($data) === 1 && isset($data['is_default']))
            {
                return show(config('code.error'), '未设置默认值');
            }
            $arr = $this->NewAstroData($param, $data, $astroConfig);
        }

        return show(config('code.success'), '加载成功', $arr);
    }

    /**
     * 更新星盘配置
     * @method POST
     * @return \think\response\Json
     */
    public function update()
    {
        $param = $this->request->only(['chart_value', 'tid', 'chart']);

        $requestData = json_decode($param['chart_value'], true);

        $requestData['chart'] = strtolower((string)$param['chart']);

        $requestData['tid'] = (int)$param['tid'];

        $checkData = $this->checkParam($requestData);

        if ($checkData !== true)
        {
            return show(config('code.error'), $checkData);
        }
        $chartTypes = [1 => '现代', 2 => '古典', 3 => '特殊'];
        // 初始化更新后的配置，保证数据纯净
        $data = self::DEFAULT_CONFIGURATION;
        // 行星数据
        foreach ($data['planet'] as $pid => $planet)
        {
            if ($pid < 10)
            {
                $pidStatus = (int)$requestData['planet']['list'][$pid]['status'];
                $data['planet'][$pid][0] = $pidStatus;
                // 容许度
                if ($requestData['tid'] !== 1)
                {
                    $pidAllowedness = (float)$requestData['planet']['list'][$pid]['allowedness'];
                    $data['planet'][$pid][1] = $pidAllowedness;
                }
            } // 虚点数据
            elseif ($pid >= 10 && $pid < 17)
            {
                $pidStatus = (int)$requestData['little_planet']['list'][$pid - 10]['status'];
                $data['planet'][$pid][0] = $pidStatus;
                // 虚点容许度
                if ($requestData['tid'] !== 1)
                {
                    $pidAllowedness = (float)$requestData['little_planet']['list'][$pid - 10]['allowedness'];
                    $data['planet'][$pid][1] = $pidAllowedness;
                }
            }//
            else
            {
                $pidStatus = (int)$requestData['four_axis']['list'][$pid - 17]['status'];
                $data['planet'][$pid][0] = $pidStatus;
                // 四轴容许度
                if ($requestData['tid'] !== 1)
                {
                    $pidAllowedness = (float)$requestData['four_axis']['list'][$pid - 17]['allowedness'];
                    $data['planet'][$pid][1] = $pidAllowedness;
                }
            }
        }

        // 希腊点数据
        if (in_array($param['chart'], ['now', 'natal', 'profection', 'firdaria', 'fortune', 'zodrel', 'dodekatemorion', 'trimsamsa', 'transits', 'synastry', 'divination']))
        {
            foreach ($data['arabic'] as $aid => $arabic)
            {
                if (isset($requestData['arabic']['list'][$aid]))
                {
                    $arabicStatus = (int)$requestData['arabic']['list'][$aid]['status'];
                    $data['arabic'][$aid][0] = $arabicStatus;
                }
                // 希腊点容许度 希腊点容许度暂不支持20190801
            }
        }

        // 相位数据
        foreach ($requestData['phase']['list'] as $k => $datum)
        {
            foreach ($data['phase'] as $phid => $phase)
            {
                if ((int)$datum['name'] === $phid)
                {
                    $data['phase'][$phid][0] = (int)$datum['status'];
                    // 相位容许度
                    if ($requestData['tid'] !== 2)
                    {
                        $data['phase'][$phid][1] = (float)$datum['allowedness'];
                    }
                }
            }
        }

        // 分宮制数据
        foreach ($requestData['h_sys']['list'] as $k => $datum)
        {
            if ((int)$datum['status'] === 1)
            {
                $data['h_sys'] = (string)$datum['thumb'];
                break;
            }
        }
        // 法达数据
        if (strcasecmp($param['chart'], 'firdaria') === 0)
        {
            foreach ($requestData['firdaria']['list'] as $k => $datum)
            {
                if ((int)$datum['status'] === 1)
                {
                    $data['firdaria'] = (int)$datum['id'];
                }
            }
        }
        // 小限推运数据
        if (strcasecmp($param['chart'], 'profection') === 0)
        {
            foreach ($requestData['profection_push_direction']['list'] as $k => $datum)
            {
                if ((int)$datum['status'] === 1)
                {
                    $data['profection_push_direction'] = (int)$datum['id'];
                }
            }
        }
        // 十度主星数据
        if (in_array($param['chart'], ['now', 'natal', 'profection', 'firdaria', 'fortune', 'zodrel', 'dodekatemorion', 'trimsamsa', 'divination']))
        {
            foreach ($requestData['decanates']['list'] as $k => $datum)
            {
                if ((int)$datum['status'] === 1)
                {
                    $data['decanates'] = (int)$datum['id'];
                }
            }
        }
        // 界主星数据
        if (in_array($param['chart'], ['now', 'natal', 'profection', 'firdaria', 'fortune', 'zodrel', 'dodekatemorion', 'trimsamsa', 'divination']))
        {
            foreach ($requestData['bound_type']['list'] as $k => $datum)
            {
                if ((int)$datum['status'] === 1)
                {
                    $data['bound_type'] = (int)$datum['id'];
                }
            }
        }

        // 返照地点选择
        if (isset($requestData['location_use']['list']) && in_array($param['chart'], ['solar_return', 'solar_returnvsnatal', 'lunar_return', 'lunar_returnvsnatal'], true))
        {
            foreach ($requestData['location_use']['list'] as $k => $datum)
            {
                if ((int)$datum['status'] === 1)
                {
                    $data['location_use'] = (int)$datum['id'];
                }
            }
        }
        // 行星中点选择 2.5.5
        if (isset($requestData['mid_type']['list']))
        {
            foreach ($requestData['mid_type']['list'] as $k => $datum)
            {
                if ((int)$datum['status'] === 1)
                {
                    $data['mid_type'] = (int)$datum['id'];
                }
            }
        }

        $param['chart_value'] = json_encode($data, true);

        $param['uid'] = (int)$this->user['id'];
        $param['tid'] = $requestData['tid'];

        if (!(new ZyhsAstroSettingUser())->updateUserSetting($param))
        {
            return show(config('code.error'), '更新配置失败');
        }

        return show(config('code.success'), '更新配置成功');
    }

    /**
     * 还原星盘配置
     * @method DELETE
     * @return \think\response\Json
     */
    public function restore()
    {
        $param = $this->request->only(['tid', 'chart']);

        $validate = validate('Astroset');

        if (!$validate->scene('delete')->check($param))
        {
            return show(config('code.error'), (string)$validate->getError());
        }

        $param['uid'] = $this->user['id'];

        if (!(new ZyhsAstroSettingUser())->restoreUserSetting($param))
        {
            return show(config('code.error'), '还原失败');
        }

        return show(config('code.success'), '还原成功');
    }

    /**
     * 新星盘数据格式重组
     * @param $param
     * @param $data
     * @param $astroConfig
     * @return array
     */
    public function NewAstroData($param, $data, $astroConfig)
    {
        $arr = [];
        $isAncient = $param['tid'] == 2;
        //行星数据
        foreach ($data['planet'] as $k => $datum)
        {
            switch (true)
            {
                //行星
                case in_array($k, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]):
                    $tmp = [
                        'id' => $k,
                        'name' => $astroConfig['planet']['name'][$k],
                        'status' => $datum[0],
                        'allowedness' => $datum[1],
                    ];
                    $arr['planet']['list'][] = $tmp;
                    $arr['planet']['select_status'] = $astroConfig['planet']['select_status'];
                    $arr['planet']['title'] = $astroConfig['planet']['title'];
                    break;
                //虚点
                case in_array($k, [10, 11, 12, 13, 14, 15, 16]):
                    $tmp = [
                        'id' => $k,
                        'name' => $astroConfig['planet']['name'][$k],
                        'status' => $datum[0],
                        'allowedness' => $datum[1],
                    ];
                    $arr['little_planet']['list'][] = $tmp;
                    $arr['little_planet']['select_status'] = $astroConfig['planet']['select_status'];
                    $arr['little_planet']['title'] = '虚点';
                    break;
                //四轴
                case in_array($k, [17, 18, 19, 20]):
                    $tmp = [
                        'id' => $k,
                        'name' => $astroConfig['planet']['name'][$k],
                        'status' => $datum[0],
                        'allowedness' => $datum[1],
                    ];
                    $arr['four_axis']['list'][] = $tmp;
                    $arr['four_axis']['select_status'] = $astroConfig['planet']['select_status'];
                    $arr['four_axis']['title'] = '四轴';
                    break;
            }
        }
        // 相位数据
        $num = 0;
        foreach ($data['phase'] as $k => $datum)
        {
//            if (Util::CheckAppVersion('2.9.5') === '小')
//            {
//                if (in_array($k, [72, 144, 108, 36])) continue;
//            }
            $tmp = [
                'id' => $num,
                'name' => $astroConfig['phase']['name'][$num] . '˚',
                'status' => $datum[0],
                'allowedness' => $datum[1],
            ];
            $arr['phase']['list'][] = $tmp;
            $arr['phase']['select_status'] = $astroConfig['phase']['select_status'];
            $num += 1;
        }
        $arr['planet']['allowedness_switch'] =
        $arr['little_planet']['allowedness_switch'] =
        $arr['four_axis']['allowedness_switch'] = $param['tid'] > 1 ? 1 : 0;
        $arr['phase']['allowedness_switch'] = $isAncient ? 0 : 1;
        $arr['phase']['title'] = '相位';
        // 希腊点数据
        if (in_array($param['chart'], ['now', 'natal', 'profection', 'firdaria', 'fortune', 'zodrel', 'dodekatemorion', 'trimsamsa', 'transits', 'synastry', 'divination']))
        {
            foreach ($data['arabic'] as $k => $datum)
            {
                // 过滤只显示到朔望点，后面的希腊点禁止开关
                if ($k > 8) break 1;

                $tmp = [
                    'id' => $k,
                    'name' => $astroConfig['arabic']['name'][$k],
                    'status' => $datum[0],
                ];
                $arr['arabic']['list'][] = $tmp;
            }
            $arr['arabic']['select_status'] = $astroConfig['arabic']['select_status'];
            $arr['arabic']['title'] = $astroConfig['arabic']['title'];
        }
        // 分宮制数据
        $num = 0;
        foreach ($astroConfig['h_sys']['name'] as $k => $datum)
        {
            $tmp = [
                'id' => $k,
                'name' => $astroConfig['h_sys']['name'][$num]['name'],
                'thumb' => $astroConfig['h_sys']['name'][$num]['thumb'],
            ];
            //十二分盘和十三分盘和福点盘默认只能整宫制
            if (in_array($param['chart'], ['fortune', 'zodrel', 'dodekatemorion', 'trimsamsa']))
            {
                if (strcasecmp($datum['thumb'], 'w') === 0)
                {
                    $tmp['status'] = 1;
                } else
                {
                    $tmp['status'] = 2;//不可选中
                }
            } else
            {
                if (strcasecmp($data['h_sys'], $datum['thumb']) === 0)
                {
                    $tmp['status'] = 1;
                } else
                {
                    $tmp['status'] = 0;
                }
            }

            $arr['h_sys']['list'][] = $tmp;

            $num += 1;
        }
        $arr['h_sys']['select_status'] = $astroConfig['h_sys']['select_status'];
        $arr['h_sys']['title'] = $astroConfig['h_sys']['title'];
        //法达数据
        if (strcasecmp($param['chart'], 'firdaria') === 0)
        {
            foreach ($astroConfig['firdaria']['name'] as $k => $datum)
            {
                $tmp = [
                    'id' => $k,
                    'name' => $astroConfig['firdaria']['name'][$k],
                    'status' => 0,
                ];
                if ($k == $data['firdaria'])
                {
                    $tmp['status'] = 1;
                }
                $arr['firdaria']['list'][] = $tmp;
            }

            $arr['firdaria']['select_status'] = $astroConfig['firdaria']['select_status'];
            $arr['firdaria']['title'] = $astroConfig['firdaria']['title'];
        }
        // 小限推运数据
        if (strcasecmp($param['chart'], 'profection') === 0)
        {
            foreach ($astroConfig['profection_push_direction']['name'] as $k => $datum)
            {
                $tmp = [
                    'id' => $k,
                    'name' => $astroConfig['profection_push_direction']['name'][$k],
                    'status' => 0,
                ];
                if ($k == $data['profection_push_direction'])
                {
                    $tmp['status'] = 1;
                }
                $arr['profection_push_direction']['list'][] = $tmp;
            }
            $arr['profection_push_direction']['select_status'] = $astroConfig['profection_push_direction']['select_status'];
            $arr['profection_push_direction']['title'] = $astroConfig['profection_push_direction']['title'];
        }
        // 十度主星数据
        if (in_array($param['chart'], ['now', 'natal', 'profection', 'firdaria', 'fortune', 'zodrel', 'dodekatemorion', 'trimsamsa', 'divination']))
        {
            foreach ($astroConfig['decanates']['name'] as $k => $datum)
            {
                $tmp = [
                    'id' => $k,
                    'name' => $astroConfig['decanates']['name'][$k],
                    'status' => 0,
                ];
                if ($k == $data['decanates'])
                {
                    $tmp['status'] = 1;
                }
                $arr['decanates']['list'][] = $tmp;
            }
            $arr['decanates']['select_status'] = $astroConfig['decanates']['select_status'];
            $arr['decanates']['title'] = $astroConfig['decanates']['title'];
        }
        // 界主星数据
        if (in_array($param['chart'], ['now', 'natal', 'profection', 'firdaria', 'fortune', 'zodrel', 'dodekatemorion', 'trimsamsa', 'divination']))
        {
            foreach ($astroConfig['bound_type']['name'] as $k => $datum)
            {
                $tmp = [
                    'id' => $k,
                    'name' => $astroConfig['bound_type']['name'][$k],
                    'status' => 0,
                ];
                if ($k == $data['bound_type'])
                {
                    $tmp['status'] = 1;
                }
                $arr['bound_type']['list'][] = $tmp;
            }
            $arr['bound_type']['select_status'] = $astroConfig['bound_type']['select_status'];
            $arr['bound_type']['title'] = $astroConfig['bound_type']['title'];
        }
        // 推运数据
        if (strcasecmp($param['chart'], 'zodrel') === 0)
        {
            foreach ($astroConfig['push_fortune']['name'] as $k => $datum)
            {
                $tmp = [
                    'id' => $k,
                    'name' => $astroConfig['push_fortune']['name'][$k],
                    'status' => 0,
                ];
                if ($k == $data['push_fortune'])
                {
                    $tmp['status'] = 1;
                }
                $arr['push_fortune']['list'][] = $tmp;
            }
            $arr['push_fortune']['select_status'] = $astroConfig['push_fortune']['select_status'];
            $arr['push_fortune']['title'] = $astroConfig['push_fortune']['title'];
        }
        // 返照地点选择
        if (in_array($param['chart'], ['solar_return', 'solar_returnvsnatal', 'lunar_return', 'lunar_returnvsnatal']))
        {
            foreach ($astroConfig['location_use']['name'] as $k => $datum)
            {
                $tmp = [
                    'id' => $k,
                    'name' => $astroConfig['location_use']['name'][$k],
                    'status' => 0,
                ];
                if ((isset($data['location_use']) && $k == $data['location_use']) || ($k === 0 && !isset($data['location_use'])))
                {
                    $tmp['status'] = 1;
                }
                $arr['location_use']['list'][] = $tmp;
            }
            $arr['location_use']['select_status'] = $astroConfig['location_use']['select_status'];
            $arr['location_use']['title'] = $astroConfig['location_use']['title'];
        }

        // 中点类型
        foreach ($astroConfig['mid_type']['name'] as $k => $v)
        {
            $tmp = [
                'id' => $k,
                'name' => $v,
                'status' => ((isset($data['mid_type']) && $k == $data['mid_type']) || ($k === 0 && !isset($data['mid_type']))) ? 1 : 0,
            ];
            $arr['mid_type']['list'][] = $tmp;
        }
        $arr['mid_type']['select_status'] = $astroConfig['mid_type']['select_status'];
        $arr['mid_type']['title'] = $astroConfig['mid_type']['title'];

        $arr['is_default'] = $data['is_default'];

        return $arr;
    }

    /**
     * 旧星盘数据重新调整
     * @param $param
     * @param $data
     * @param $astroConfig
     * @return array
     */
    public function OldAstroData($param, $data, $astroConfig)
    {
        $arr = [];
        // 相位数据
        foreach ($data['asp_orb_all'] as $k => $a)
        {
            // 相位开关默认不显示
            $status = 0;
            // 相位的开关
            foreach ($data['asps_display'] as $b)
            {
                if ($b == $k)
                {
                    $status = 1;
                }
            }

            // 相位数据重组
            foreach ($astroConfig['phase']['name'] as $kk => $item)
            {
                if (in_array((int)$item, [72, 144])) continue;

                if ($k == $item)
                {
                    $arr['phase']['list'][] = [
                        'id' => $kk,
                        'name' => $item . '°',
                        'status' => $status,
                        'allowedness' => $a,
                    ];
                }
            }
        }
        // 相位选项类型,1=>单选,1=>多选
        $arr['phase']['select_status'] = $astroConfig['phase']['select_status'];
        $isAncient = $param['tid'] == 2;
        // 相位容许度开关,默认古典盘不显示
        $arr['phase']['title'] = '相位';

        // 行星数据
        $strLen = strlen($data['planets_display']);
        for ($i = 0; $i < $strLen; $i++)
        {
            switch (true)
            {
                // 行星
                case in_array($i, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]):
                    if ($data['planets_display'][$i] == 1)
                    {
                        $planet_status = 1;
                    } else
                    {
                        $planet_status = 0;
                    }
                    $arr['planet']['list'][] = [
                        'id' => $i,
                        'name' => $astroConfig['planet']['name'][$i],
                        'status' => $planet_status,
                        'allowedness' => $data['planet_orb_all'][$i] ?? 0
                    ];
                    $arr['planet']['select_status'] = $astroConfig['planet']['select_status'];
                    $arr['planet']['title'] = '行星';
                    break;
                // 虚点
                case in_array($i, [10, 11, 12, 13, 14, 15, 16]):
                    if ($data['planets_display'][$i] == 1)
                    {
                        $planet_status = 1;
                    } else
                    {
                        $planet_status = 0;
                    }
                    $arr['little_planet']['list'][] = [
                        'id' => $i,
                        'name' => $astroConfig['planet']['name'][$i],
                        'status' => $planet_status,
                        'allowedness' => 0,
                    ];
                    $arr['little_planet']['select_status'] = $astroConfig['planet']['select_status'];
                    $arr['little_planet']['title'] = '虚点';
                    break;
                // 四轴
                case in_array($i, [17, 18, 19, 20]):
                    if ($data['planets_display'][$i] == 1)
                    {
                        $planet_status = 1;
                    } else
                    {
                        $planet_status = 0;
                    }
                    $arr['four_axis']['list'][] = [
                        'id' => $i,
                        'name' => $astroConfig['planet']['name'][$i],
                        'status' => $planet_status,
                        'allowedness' => 0
                    ];
                    $arr['four_axis']['select_status'] = $astroConfig['planet']['select_status'];
                    $arr['four_axis']['title'] = '四轴';
                    break;
            }

        }
        // 希腊点数据
        if (in_array($param['chart'], ['now', 'natal', 'profection', 'firdaria', 'fortune', 'zodrel', 'dodekatemorion', 'trimsamsa', 'transits', 'synastry', 'divination']) && isset($data['arabic']))
        {
            foreach ($data['arabic'] as $k => $datum)
            {
                if ($k > 8) break 1;
                $status = $datum[0] ? 1 : 0;
                $arr['arabic']['list'][] = [
                    'id' => $k,
                    'name' => $astroConfig['arabic']['name'][$k],
                    'status' => $status,
                    'allowedness' => 0
                ];
            }
            $arr['arabic']['select_status'] = $astroConfig['arabic']['select_status'];
            $arr['arabic']['title'] = $astroConfig['arabic']['title'];
        }

        $arr['planet']['allowedness_switch'] =
        $arr['little_planet']['allowedness_switch'] =
        $arr['four_axis']['allowedness_switch'] = $param['tid'] > 1 ? 1 : 0;
        $arr['phase']['allowedness_switch'] = $isAncient ? 0 : 1;
        // 分宮制
        foreach ($astroConfig['h_sys']['name'] as $k => $datum)
        {
            // 十二分盘和十三分盘和福点盘默认只能整宫制
            if (in_array($param['chart'], ['fortune', 'zodrel', 'dodekatemorion', 'trimsamsa']))
            {
                if (strcasecmp($datum['thumb'], 'w') === 0)
                {
                    $status = 1;
                } else
                {
                    $status = 2;// 不可选中
                }
            } else
            {
                if ($data['h_sys'] == $datum['thumb'])
                {
                    $status = 1;
                } else
                {
                    $status = 0;
                }
            }

            $arr['h_sys']['list'][] = [
                'id' => $k,
                'name' => $datum['name'],
                'thumb' => $datum['thumb'],
                'status' => $status
            ];
        }
        $arr['h_sys']['select_status'] = $astroConfig['h_sys']['select_status'];
        $arr['h_sys']['title'] = $astroConfig['h_sys']['title'];

        // 法达星序
        if (strcasecmp($param['chart'], 'firdaria') === 0 && isset($data['firdaria']))
        {
            foreach ($astroConfig['firdaria']['name'] as $k => $datum)
            {
                if ($data['firdaria'] == $k)
                {
                    $status = 1;
                } else
                {
                    $status = 0;
                }

                $arr['firdaria']['list'][] = [
                    'id' => $k,
                    'name' => $datum,
                    'status' => $status
                ];
            }
            $arr['firdaria']['select_status'] = $astroConfig['firdaria']['select_status'];
            $arr['firdaria']['title'] = $astroConfig['firdaria']['title'];
        }

        // 小限推运方向
        if (strcasecmp($param['chart'], 'profection') === 0 && isset($data['profection_push_direction']))
        {
            foreach ($astroConfig['profection_push_direction']['name'] as $k => $datum)
            {
                if ($data['profection_push_direction'] == $k)
                {
                    $status = 1;
                } else
                {
                    $status = 0;
                }

                $arr['profection_push_direction']['list'][] = [
                    'id' => $k,
                    'name' => $datum,
                    'status' => $status
                ];
            }
            $arr['profection_push_direction']['select_status'] = $astroConfig['profection_push_direction']['select_status'];
            $arr['profection_push_direction']['title'] = $astroConfig['profection_push_direction']['title'];
        }
        // 十度主星
        if (in_array($param['chart'], ['now', 'natal', 'profection', 'firdaria', 'fortune', 'zodrel', 'dodekatemorion', 'trimsamsa', 'divination']) && isset($data['decanates']))
        {
            foreach ($astroConfig['decanates']['name'] as $k => $datum)
            {
                if ($data['decanates'] == $k)
                {
                    $status = 1;
                } else
                {
                    $status = 0;
                }

                $arr['decanates']['list'][] = [
                    'id' => $k,
                    'name' => $datum,
                    'status' => $status
                ];
            }
            $arr['decanates']['select_status'] = $astroConfig['decanates']['select_status'];
            $arr['decanates']['title'] = $astroConfig['decanates']['title'];
        }
        // 界主星
        if (in_array($param['chart'], ['now', 'natal', 'profection', 'firdaria', 'fortune', 'zodrel', 'dodekatemorion', 'trimsamsa', 'divination']) && isset($data['bound_type']))
        {
            foreach ($astroConfig['bound_type']['name'] as $k => $datum)
            {
                if ($data['bound_type'] == $k)
                {
                    $status = 1;
                } else
                {
                    $status = 0;
                }

                $arr['bound_type']['list'][] = [
                    'id' => $k,
                    'name' => $datum,
                    'status' => $status
                ];
            }
            $arr['bound_type']['select_status'] = $astroConfig['bound_type']['select_status'];
            $arr['bound_type']['title'] = $astroConfig['bound_type']['title'];
        }
        // 推运
        if (strcasecmp($param['chart'], 'zodrel') === 0 && isset($data['push_fortune']))
        {
            foreach ($astroConfig['push_fortune']['name'] as $k => $datum)
            {
                if ($data['push_fortune'] == $k)
                {
                    $status = 1;
                } else
                {
                    $status = 0;
                }

                $arr['push_fortune']['list'][] = [
                    'id' => $k,
                    'name' => $datum,
                    'status' => $status
                ];
            }
            $arr['push_fortune']['select_status'] = $astroConfig['push_fortune']['select_status'];
            $arr['push_fortune']['title'] = $astroConfig['push_fortune']['title'];
        }

        // 返照地点选择
        if (in_array($param['chart'], ['solar_return', 'solar_returnvsnatal', 'lunar_return', 'lunar_returnvsnatal'], true))
        {
            foreach ($astroConfig['location_use']['name'] as $k => $datum)
            {
                $tmp = [
                    'id' => $k,
                    'name' => $astroConfig['location_use']['name'][$k],
                    'status' => 0,
                ];
                if ((isset($data['location_use']) && $k == $data['location_use']) || ($k === 0 && !isset($data['location_use'])))
                {
                    $tmp['status'] = 1;
                }
                $arr['location_use']['list'][] = $tmp;
            }
            $arr['location_use']['select_status'] = $astroConfig['location_use']['select_status'];
            $arr['location_use']['title'] = $astroConfig['location_use']['title'];
        }

        // 中点类型
        foreach ($astroConfig['mid_type']['name'] as $k => $v)
        {
            $tmp = [
                'id' => $k,
                'name' => $v,
                'status' => ((isset($data['mid_type']) && $k == $data['mid_type']) || ($k === 0 && !isset($data['mid_type']))) ? 1 : 0,
            ];
            $arr['mid_type']['list'][] = $tmp;
        }
        $arr['mid_type']['select_status'] = $astroConfig['mid_type']['select_status'];
        $arr['mid_type']['title'] = $astroConfig['mid_type']['title'];

        $arr['is_default'] = $data['is_default'];
        return $arr;
    }

    /**
     * 验证客户端传递的参数是否正确
     * @param array $param
     * @return bool|string
     */
    private function checkParam($param = [])
    {
        try
        {
            if (!isset($param['chart']) || !in_array($param['chart'], config('astroset175.chart'), true))
            {
                return '未知星盘';
            }
            if (count($param['planet']['list']) !== 10)
            {
                return '行星设置失败';
            }

            if (count($param['phase']['list']) !== 13)
            {
                return '相位设置失败';
            }

            if (count($param['little_planet']['list']) !== 7)
            {
                return '虚点设置失败';
            }

            if (count($param['four_axis']['list']) !== 4)
            {
                return '四轴设置失败';
            }

            if (count($param['h_sys']['list']) !== 12)
            {
                return '分宫制设置失败';
            }

            // 包含界主星和十度主星的星盘
            $ContainBoundDecanatesCharts = ['natal', 'now', 'profection', 'firdaria', 'fortune', 'zodrel', 'dodekatemorion', 'trimsamsa', 'divination'];

            // 包含希腊点的星盘
            $ContainArabicsCcharts = ['natal', 'now', 'profection', 'firdaria', 'fortune', 'zodrel', 'dodekatemorion', 'trimsamsa', 'transits', 'synastry', 'divination'];

            // 涉及行星中点的星盘
            $ContainMidPlanetCharts = [''];

            // 黄道星释盘验证
            if ($param['chart'] === 'zodrel' && (!isset($param['push_fortune']) || count($param['push_fortune']) > 20))
            {
                return '推运基点参数错误';
            }

            // 小限盘参数验证
            if ($param['chart'] === 'profection' && (!isset($param['profection_push_direction']['list']) || count($param['profection_push_direction']['list']) !== 2))
            {
                return '小限推运方向参数错误';
            }

            if (in_array($param['chart'], $ContainBoundDecanatesCharts, true) && (!isset($param['bound_type']['list']) || count($param['bound_type']['list']) !== 2))
            {
                return '界主星参数错误';
            }

            if (in_array($param['chart'], $ContainBoundDecanatesCharts, true) && (!isset($param['decanates']['list']) || count($param['decanates']['list']) !== 2))
            {
                return '十度主星参数错误';
            }

            if (in_array($param['chart'], $ContainArabicsCcharts, true) && (!isset($param['arabic']['list']) || count($param['arabic']['list']) !== 9))
            {
                return '希腊点参数错误';
            }
            // 返照地点参数验证
            if (in_array($param['chart'], ['solar_return', 'solar_returnvsnatal', 'lunar_return', 'lunar_returnvsnatal'], true) && (!isset($param['location_use']['list']) || count($param['location_use']['list']) !== 2))
            {
                return '返照地点参数错误';
            }
        }
        catch (\Exception $e)
        {
            return $e->getMessage();
        }

        return true;
    }
}
